$(function(){

	//绑定机构下拉列表
	bindOrg2('org');
	
	var waterwaycode = getUrlParam("waterwaycode");
	
	bindForm(waterwaycode);
})


function bindForm(waterwaycode){
	$.ajax({ 
		url: basePath() + 'waterway/getinfo.do',
		data: {'waterwaycode': waterwaycode},
		dataType: "json",
        contentType: "application/json",
		success: function(data){	
	    	if(data.result == 1){
    			for(var d in data.datas){
    				$('[name="'+ d +'"]').val(data.datas[d]);
    				
    				if(d == 'orgcode'){
    					$('#org').attr('data', data.datas[d]);
    					$('#org').val(data.datas["orgname"]);
    				}
    			}
	    	} 
	    	else{
	    		window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.error);
	    	}
      	}
	});
}

function save(){
	var waterwayData = $("#waterwayForm").serializeObject();
	waterwayData['orgcode'] = $('#org').attr('data');

	if(!check(waterwayData))
		return true;
	
	$.ajax({ 
		method: 'post',
		url: basePath() + 'waterway/update.do',
		data: JSON.stringify(waterwayData),
		dataType: "json",
        contentType: "application/json",
		success: function(data){	
	    	if(data.result == 1){
				window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success,{
					onOk:function(v){
						parent.refreshData();		//刷新数据
					}
				});
	    	} 
	    	else{
	    		window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.error);
	    	}
      	}
	});
}

function check(data){	
	var result = true;
	
	var mustStr = "waterwaycode,waterwayname,orgcode";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
	

	
	
	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	
	return result;
}
